<?php

class DownloadFile
{
    public static function downloadFromUrl($fromUrl, $saveToPath, $bufferLength = -1)
    {
        try {
            if ($bufferLength < 0) {
                $bufferLength = 1024 * 8; // Default buffer length: 8KB
            }

            $newfname = $saveToPath;
            $file = fopen($fromUrl, "rb");

            if ($file) {
                $newf = fopen($newfname, "wb");

                if ($newf)
                    while (!feof($file)) {
                        fwrite($newf, fread($file, $bufferLength), $bufferLength);
                    }
            }

            if ($file) {
                fclose($file);
            }

            if ($newf) {
                fclose($newf);
            }

            return true;
        } catch (Exception $e) {
//            Debug::dump($e);
            return false;
        }
    }
}